<template>
  <div class="login-container">
    <div class="flex justify-end">
      <NuxtLink to="/">
        <span class="f-16">随便看看</span>
      </NuxtLink>
    </div>
    <div class="header flex flex-column justify-center align-center">
      <span class="logo" />
      <div class="f-16">
        登录博MOCK
      </div>
      <div class="f-14">
        分享生活 发现世界
      </div>
    </div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="phone"
        :error="false"
        clearable
        name="phone"
        label=""
        placeholder="输入手机号"
        :rules="[
          { required: true, message: '请输入手机号' },
          {
            required: true,
            validator: phoneValidator,
            message: '请输入正确的手机号',
          },
        ]"
      />
      <van-field
        v-model="password"
        :error="false"
        clearable
        type="password"
        name="password"
        label=""
        placeholder="输入密码"
        :rules="[
          { required: true, message: '请输入密码' },
          {
            required: true,
            validator: passwordLengthValidator,
            message: '密码长度为至少为6位',
          },
          ,
        ]"
      />

      <van-button
        size="small"
        :style="{ opacity: phone && password ? 1 : 0.5 }"
        class="get-vcode"
        round
        block
        ative-type="submit"
      >
        登录
      </van-button>
    </van-form>
    <NuxtLink to="/login">
      <van-button
        size="small"
        class="account-login"
        round
        block
      >
        用短信验证码登录
      </van-button>
    </NuxtLink>
    <div class="flex mt-20 justify-between">
      <NuxtLink to="/login/forgetPassword">
        <span class="f-14 color-blue">忘记密码</span>
      </NuxtLink>
      <!-- <NuxtLink to="/register">
          <span class="f-14 color-blue">注册</span>
        </NuxtLink> -->
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      phone: '',
      password: ''
    }
  },
  methods: {
    // 手机号校验
    phoneValidator (value, rule) {
      const reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      return reg_tel.test(value)
    },
    // 密码长度校验
    passwordLengthValidator (value, rule) {
      return value.length >= 6
    },
    async onSubmit (values) {
      const result = await this.$axios.post('/v1/login/accountPasswordLogin', values)
      if (result && result.data.code == 200) {
        this.$router.push(this.$route.query.url?this.$route.query.url:'/')
        this.$store.commit('setLogin', true)
        this.$cookies.set("userInfo",result.data.result)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  padding: 10px;
  .close {
    font-size: 25px;
  }
  .header {
    margin-bottom: 50px;
    .logo {
      margin-bottom: 10px;
      width: 50px;
      height: 50px;
      background: url("@/assets/imgs/logo.jpg");
      background-size: 100% 100%;
    }
  }
  .get-vcode {
    margin-top: 20px;
    color: #fff;
    font-size: 14px;
    background: linear-gradient(#fccf31, #f55555);
  }
  .account-login {
    font-size: 14px;
    margin-top: 20px;
    color: #f55555;
    border-color: #f55555;
    background: #fff;
  }
  .color-blue {
    color: #4169e1;
  }
}
</style>
